# Copyright (C) 2010 Tom Verbeure
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require 'csv'
require 'gdata'

module GData
    module Client
        class Tables < Base

            SERVICE_URL = "http://tables.googlelabs.com/api/query"

            def initialize(options = {})
                options[:clientlogin_service] ||= 'fusiontables'
                options[:headers] = { 'Content-Type' => 'application/x-www-form-urlencoded' }
                super(options)
            end

            def sql_encode(sql)
                "sql=" + CGI::escape(sql)
            end

            def sql_get(sql)
                resp = self.get(SERVICE_URL + "?" + sql_encode(sql))
            end

            def sql_post(sql)
                resp = self.post(SERVICE_URL, sql_encode(sql))
            end

            def sql_put(sql)
                resp = self.put(SERVICE_URL, sql_encode(sql))
            end

            # Create a new table. Return the table id
            def create_table(sql)
                resp = self.sql_post(sql)
                table_id = resp.body.split("\n")[1].chomp.to_i
            end

            # Returns a hash with the definitions of all columns 
            def describe_table(table_id)
                resp = self.sql_get("DESCRIBE #{table_id}")
                
                columns = {}
                first = true
                CSV::Reader.parse(resp.body) do |row|
                    if first
                        first = false
                        next
                    end

                    columns[row[1]] = { :id => row[0], :type => row[2] }
                end

                return columns
            end
        end
    end
end

